package com.cindata.dictionary

import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

@Secured(['isAuthenticated()'])
@Transactional
class MortgageCategoryController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index()
    {
        def max = Math.min(params.max ? params.max.toInteger() : 10, 100)
        def offset = params.offset ? params.offset.toInteger() : 0

        def code = params['code']
        def name = params['name']

        def sql = "from MortgageCategory where 1=1 "
        if (code)
        {
            sql += " and code like '%${code}%'"
        }
        if (name)
        {
            sql += " and name like '%${name}%'"
        }

        def countSql = "select count(id) " + sql
        sql += " order by name desc "

        def mortgageCategoryList = MortgageCategory.executeQuery(sql, [max: max, offset: offset])
        def mortgageCategoryCount = MortgageCategory.executeQuery(countSql)
        respond mortgageCategoryList, model: [mortgageCategoryCount: mortgageCategoryCount[0], params: params], view: 'index'
    }

    def show(MortgageCategory mortgageCategory) {
        respond mortgageCategory
    }

    def create() {
        respond new MortgageCategory(params)
    }

    @Transactional
    def save(MortgageCategory mortgageCategory) {
        if (mortgageCategory == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (mortgageCategory.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond mortgageCategory.errors, view:'create'
            return
        }

        mortgageCategory.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'mortgageCategory.label', default: 'MortgageCategory'), mortgageCategory.id])
                redirect mortgageCategory
            }
            '*' { respond mortgageCategory, [status: CREATED] }
        }
    }

    def edit(MortgageCategory mortgageCategory) {
        respond mortgageCategory
    }

    @Transactional
    def update(MortgageCategory mortgageCategory) {
        if (mortgageCategory == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (mortgageCategory.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond mortgageCategory.errors, view:'edit'
            return
        }

        mortgageCategory.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'mortgageCategory.label', default: 'MortgageCategory'), mortgageCategory.id])
                redirect mortgageCategory
            }
            '*'{ respond mortgageCategory, [status: OK] }
        }
    }

    @Transactional
    def delete(MortgageCategory mortgageCategory) {

        if (mortgageCategory == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        mortgageCategory.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'mortgageCategory.label', default: 'MortgageCategory'), mortgageCategory.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'mortgageCategory.label', default: 'MortgageCategory'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
